#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# 模型表单
from django import forms
from .models import Student, StudentDetail, Course, Grade, Enroll

class StudentForm(forms.ModelForm):

    class Meta:

        model = Student
        exclude = ['is_delete'] # 排除前端显示的字段
        # fields = ['name', 'age'] # 指定显示字段
        # fields = '__all__' # 显示所有字段
        widgets = {'sex': forms.RadioSelect} # 指定字段为单选按钮类型

class StudentDetailForm(forms.ModelForm):

    class Meta:

        model = StudentDetail
        exclude = ['student'] # 表关系前端不需要显示

    def clean_num(self): # 模型表单，简单标签命名 clean_+字段名

        data = self.cleaned_data.get('num')
        if not data[:-1].isdigit():
            # self.add_error('num', '身份证号码格式有误')
            raise forms.ValidationError('身份证号码格式有误')